import {
    reqPlayListDetail,
    reqMusicDetail
} from '@/api'
const state = {
    playlistDetail: {
    },
    // 是否正在加载
    loading:false
}

const mutations = {
    PLAYLISTDETAIL(state, playlistDetail) {
        state.playlistDetail = playlistDetail
    },
    LOADING(state, flag){
        state.loading = flag
    }
}

const actions = {
    async playlistDetail({commit}, id) {
        commit('LOADING', true)
        let result = await reqPlayListDetail(id)
        // 取出所有的歌曲id
        let ids = result.playlist.trackIds.map(item => {
            return item.id
        })
        let musicList = await reqMusicDetail(ids.toString())
        result.playlist.musicList = musicList
        if (result.code == 200) {
            commit("PLAYLISTDETAIL", result.playlist)
        }
        commit('LOADING',false)
    }
}

function filterArtists(value)  {
    // 过滤多个歌手名
    let name = value[0].name;
    if (value.length == 1) {
        return name;
    }
    // 长度大于一
    for(let i = 1; i<value.length ; i++){
        name += " / " + value[i].name
    }
    return name;
}

const getters = {
    // 表格数据
    musicListTableData: (state) => {
        // console.log(state.playlistDetail.musicList.songs)
        let musicList = state.playlistDetail.musicList.songs.map(item => {
            return {
                id: item.id,
                title: item.name,
                artist: filterArtists(item.ar),
                album: item.al.name,
            }
        })
        return musicList
    }
}

export default {
    state,
    mutations,
    actions,
    getters
}